#include "stm32f10x.h"                 
#include "Delay.h"
#include "OLED.h"
#include "BlueTooth.h"
#include "Servo.h"
#include "PetAction.h"
#include "Face_Config.h"
#include "PWM.h"

uint16_t Time;
uint16_t HuXi;
uint16_t PanDuan=1;
uint16_t Wait=0;


// 定义动作函数的类型
typedef void (*ActionFunction)();

// 定义一个动作函数指针数组，将每个Action_Mode对应到相应的动作函数
ActionFunction actions[] = {
    Action_relaxed_getdowm,   // Action_Mode = 0		//放松趴下
    Action_sit,               // Action_Mode = 1		//坐下
    Action_upright,           // Action_Mode = 2		//站立
    Action_getdowm,           // Action_Mode = 3		//趴下
    Action_advance,           // Action_Mode = 4		//前进
    Action_back,              // Action_Mode = 5		//后退
    Action_Lrotation,         // Action_Mode = 6		//左转
    Action_Rrotation,         // Action_Mode = 7		//右转
    Action_Swing,             // Action_Mode = 8		//摇摆
    Action_SwingTail,         // Action_Mode = 9		//摇尾巴
    Action_JumpU,             // Action_Mode = 10		//前跳
    Action_JumpD,             // Action_Mode = 11		//后跳
    Action_upright2,          // Action_Mode = 12		//站立方式2
    Action_Hello,             // Action_Mode = 13		//打招呼
    Action_stretch,           // Action_Mode = 14		//伸懒腰
    Action_Lstretch           // Action_Mode = 15		//后腿拉伸
};

int main(void)
{
	Servo_Init();
	OLED_Init();//OLED初始化
	BlueTooth_Init();//蓝牙初始化
	OLED_ShowImage(0,0,128,64,Face_sleep);
	OLED_Update();
	while(1)
	{	
		if(Action_Mode <= 15) {
		// 通过Action_Mode调用对应的动作函数
			actions[Action_Mode]();
		}
	}
}


void TIM3_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET)
	{	
		if(AllLed==1 && BreatheLed==0)//如果灯光开启且不开启呼吸灯,亮度拉满
		{
			PWM_LED1(20000);
			PWM_LED2(20000);
		}
		else if(AllLed==1 && BreatheLed==1)//如果灯光开启且开启呼吸灯
		{
			if(PanDuan==1)
			{
				HuXi+=100;
				PWM_LED1(HuXi);
				PWM_LED2(HuXi);
				if(HuXi==20000)
					PanDuan=2;
			}
			else if(PanDuan==2)
			{
				HuXi-=100;
				PWM_LED1(HuXi);
				PWM_LED2(HuXi);
				if(HuXi==0)
				{
					PanDuan=3;
				}
			}
			else if(PanDuan==3)
			{
				Wait+=1000;
				if(Wait==20000)
				{
					PanDuan=1;
					Wait=0;
				}
			}
		}
		else if(AllLed==0)
		{
			PWM_LED1(0);
			PWM_LED2(0);
		}
		
		TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
	}
}
